<?php
namespace App\Libraries\Decorator\Order;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Image\ImagesDecorator;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Tools;
use App\Libraries\Tools\URL;
use App\Models\HongBao\HongBao;
use App\Models\Order\Order;
use App\Models\Post\Post;
use App\Models\Product\Product;
use App\Models\Product\ProductPurchase;
use App\Models\Shop\Shop;
use App\Models\Shop\ShopCoupon;
use App\Models\Shop\ShopCouponPurchase;
use App\Models\User\User;
use Illuminate\Support\Facades\Cache;

class OrderDecoratorV2 implements ModelDecorator
{

    public function before(){
        //todo
    }

    public function after(&$data){
        if(!is_array($data)){
            $data = json_decode(json_encode($data), true);
        }
        if (isset($data['list'])) {
            // 一维数组
            foreach($data['list'] as &$v){
                switch($v['type']){
                    case ConstVar::$ORDER_TYPE_POST:
                        $post = new Post();
                        $post->addDecorator(new ImagesDecorator());
                        $post->addDecorator(new CoverDecorator());
                        $v['object'] = $post->selectRaw('user_id,shop as shop_name,category,images,post,price')
                            ->withOnly('user', ['user_id', 'nickname', 'cover', 'sex', 'vip_level'])
                            ->where('id', $v['object_id'])->first();
                        $post->after($v['object']);
                        break;
                    case ConstVar::$ORDER_TYPE_PRODUCT:
                        $product = new ProductPurchase();
                        $product->addDecorator(new CoverDecorator());
                        $v['object'] = $product->selectRaw('id as purchase_id,title,point,money,cover,shipping_status')->where('order_id', $v['id'])->first();
                        $product->after($v['object']);
                        break;
                    case ConstVar::$ORDER_TYPE_TRANSFER_TO_SHOP:
                        $shop = new Shop();
                        $shop->addDecorator(new CoverDecorator());
                        $v['object'] = $shop->select('shop_id','title','type','cover','address','phone')->where('shop_id', $v['object_id'])->first();
                        $shop->after($v['object']);
                        break;
                    case ConstVar::$ORDER_TYPE_COUPON:
                        $shopCouponPurchase = new ShopCouponPurchase();
                        $shopCouponPurchase->addDecorator(new CoverDecorator());
                        $v['object'] = $shopCouponPurchase->selectRaw('id as purchase_id,shop_id,code,title,content,cover,rec_num,price,amount,status,start_at,end_at')
                            ->withOnly('shop', ['shop_id','title','type','cover'])
                            ->where('order_id', $v['id'])->first();
                        $shopCouponPurchase->after($v['object']);
                        break;
                    case ConstVar::$ORDER_TYPE_TRANSFER_TO_USER:
                        $user = new User();
                        $user->addDecorator(new CoverDecorator());
                        $v['object'] = $user->select('user_id','nickname','cover','sex','vip_level','vip_validity')->where('user_id', $v['object_id'])->first();
                        $user->after($v['object']);
                        break;
                    case ConstVar::$ORDER_TYPE_HONGBAO:
                        $hongbao = new HongBao();
                        $v['object'] = $hongbao->select('origin','type','status','point','limit_point','money','limit_money','end_at')->where('id', $v['object_id'])->first();
                        break;
                    default:
                        break;
                }
                $v['payment_info'] = Cache::get($v['out_trade_no']);
                if(empty($v['payment_info']) && $v['status'] == ConstVar::$ORDER_STATUS_WAITING){
                    $order = Order::where('id', $v['id'])->first();
                    $order->status = ConstVar::$ORDER_STATUS_EXPIRED;
                    $order->save();
                    $v['status'] = ConstVar::$ORDER_STATUS_EXPIRED;
                }
            }
        }
        if(is_array($data)){
            foreach($data as &$v){
                $this->after($v);
            }
        }
        return $data;
    }
}